package com.qzsoft.system.modules.syslogerror.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.system.modules.syslogerror.entity.ZhjdSysLogErrorEntity;
import com.qzsoft.system.modules.syslogerror.service.ZhjdSysLogErrorService;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.excel.ZhjdExcelExportUtils;
import com.qzsoft.zhjd.common.excel.style.ZhjdExcelExportStyler;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;

import cn.afterturn.easypoi.excel.entity.ExportParams;

import com.qzsoft.system.modules.syslogerror.mapper.ZhjdSysLogErrorMapper;

/**
 * @ClassName ZhjdSysLogErrorServiceImpl
 * @Description 错误日志业务接口实现
 * @author linhuawei
 * @since 2022-05-16
 */
@Service
public class ZhjdSysLogErrorServiceImpl extends ServiceImpl<ZhjdSysLogErrorMapper, ZhjdSysLogErrorEntity>
        implements ZhjdSysLogErrorService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Override
    public IPage<ZhjdSysLogErrorEntity> page(Map<String, Object> params) {
        IPage<ZhjdSysLogErrorEntity> page = baseServiceComponent.getPage(params, ZhjdSysLogErrorEntity.class);
        QueryWrapper<ZhjdSysLogErrorEntity> queryWrapper = getQueryWrapper(params);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public void export(Map<String, Object> params, HttpServletResponse response) {
        QueryWrapper<ZhjdSysLogErrorEntity> queryWrapper = getQueryWrapper(params);
        List<ZhjdSysLogErrorEntity> list = baseMapper.selectList(queryWrapper);
        String fileName = "错误日志";
        ExportParams exportParams = new ExportParams(null, fileName);
        exportParams.setFixedTitle(false);
        exportParams.setStyle(ZhjdExcelExportStyler.class);
        try {
            ZhjdExcelExportUtils.exportExcel(response, fileName, exportParams, ZhjdSysLogErrorEntity.class, list);
        } catch (IOException e) {
            log.error("错误日志导出失败：{}", e);
            RenExceptionUtils.throwEx("数据导出失败");
        }

    }

    /**
     * @Title getQueryWrapper
     * @Description 处理查询条件
     * @param params 查询条件
     * @return 查询条件
     */
    private QueryWrapper<ZhjdSysLogErrorEntity> getQueryWrapper(Map<String, Object> params) {
        QueryWrapper<ZhjdSysLogErrorEntity> queryWrapper = new QueryWrapper<ZhjdSysLogErrorEntity>();
        if (ObjectUtils.isNotEmpty(params.get("startDate"))) {
            queryWrapper.ge("date_format(create_date,'%Y-%m-%d')", params.get("startDate"));
        }
        if (ObjectUtils.isNotEmpty(params.get("endDate"))) {
            queryWrapper.le("date_format(create_date,'%Y-%m-%d')", params.get("endDate"));
        }
        queryWrapper.orderByDesc("create_date");
        return queryWrapper;
    }

}
